package com.snail.readyvr.activity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.snail.readyvr.R;
import com.snail.readyvr.application.MyApplication;
import com.snail.readyvr.di.component.DaggerSettingsActivityPresenterComponent;
import com.snail.readyvr.di.module.SettingsActivityPresenterModule;
import com.snail.readyvr.dialog.LanguageDialog;
import com.snail.readyvr.event.LoginEvent;
import com.snail.readyvr.mvp.contract.SettingsActivityContract;
import com.snail.readyvr.mvp.model.User;
import com.snail.readyvr.mvp.presenter.SettingsActivityPresenter;
import com.snail.readyvr.ui.CommonTopBar;
import com.snail.readyvr.util.ACache;
import com.snail.readyvr.util.Constant;
import com.snail.readyvr.util.DialogUtils;
import com.snail.readyvr.util.IntentParams;
import com.snail.readyvr.util.SPUtils;
import com.snail.readyvr.util.StartActivityUtils;
import com.snail.readyvr.util.StringUtils;
import com.snail.readyvr.util.ToastUtils;
import com.snail.readyvr.util.Utils;

import javax.inject.Inject;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by MRKING on 2017/1/17.
 */

public class SettingsActivity extends CheckPermissionsActivity implements CommonTopBar.OnClickCommonTopBar, SettingsActivityContract.View {
    @BindView(R.id.settings_top_bar)
    CommonTopBar mSettingsTopBar;
    @BindView(R.id.settings_language_text)
    TextView mSettingsLanguageText;
    @BindView(R.id.settings_language_layout)
    RelativeLayout mSettingsLanguageLayout;
    @BindView(R.id.settings_version_update_img)
    ImageView mSettingsVersionUpdateImg;
    @BindView(R.id.settings_cache_text)
    TextView mSettingsCacheText;
    @BindView(R.id.login_out)
    Button mLogout;

    @BindArray(R.array.language_names)
    String[] mNames;

    @Inject
    SettingsActivityPresenter mPresenter;

    private Dialog deleteCacheDialog;

    private int languageIndex = 0;

    @Override
    protected void hasGetAllPermissions() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_settings;
    }

    @Override
    protected void init() {
        if (Utils.sUser != null && !StringUtils.isBlank(Utils.sUser.getToken())) {
            mLogout.setClickable(true);
            mLogout.setVisibility(View.VISIBLE);
        }else {
            mLogout.setClickable(false);
            mLogout.setVisibility(View.INVISIBLE);
        }
        String languageName = (String) SPUtils.get(this,Constant.Language.LANGUAGE_KEY, "");

        if (languageName == null || languageName.equals(""))
            languageName = Utils.getDefaultLanguage(this);

        languageIndex = Utils.getLanguageIndex(languageName);
        if(languageIndex < mNames.length)
            mSettingsLanguageText.setText(mNames[languageIndex]);
        mSettingsTopBar.setOnClickCommonTopBar(this);
        mSettingsTopBar.midTextView.setText(R.string.setting);
        DaggerSettingsActivityPresenterComponent.builder().applicationComponent(((MyApplication) getApplication()).getApplicationComponent())
                .settingsActivityPresenterModule(new SettingsActivityPresenterModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getCacheLength();
    }

    @OnClick({R.id.settings_version_layout, R.id.settings_cache_layout, R.id.settings_about, R.id.login_out, R.id.settings_language_layout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.settings_language_layout:
                LanguageDialog.Builder builder = new LanguageDialog.Builder(this);
                builder.addSingleChoiceItems(mNames, getString(R.string.settings_language_dialog_title) , languageIndex, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        if(which == languageIndex)
                            return;
                        languageIndex = which;
                        String name = Utils.getLanuageByIndex(languageIndex);
                        if(name != null) {
                            ToastUtils.show(SettingsActivity.this, R.string.settings_change_language_tip);
                            SPUtils.put(SettingsActivity.this, Constant.Language.LANGUAGE_KEY, name);
                           // Utils.changeLanuage(SettingsActivity.this, name);
                            Intent intent = new Intent(SettingsActivity.this, WelcomeActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            SettingsActivity.this.startActivity(intent);
                            SettingsActivity.this.finish();
                        }
                    }
                });
                builder.show();
                break;
            case R.id.settings_version_layout:
                //DialogUtils.showCheckUpdate(this,getString(R.string.settings_version_update_tip));
                break;
            case R.id.settings_cache_layout:
                deleteCacheDialog = DialogUtils.showCheckUpdate(this,getString(R.string.settings_cache_tip));
                mPresenter.deleteCache();
                break;
            case R.id.settings_about:
                StartActivityUtils.startAboutActivity(this);
                overridePendingTransition(R.anim.scale_dialog_enter, 0);
                break;
            case R.id.login_out:
                DialogUtils.showEmialTip(this, getString(R.string.login_out_title),
                        getString(R.string.login_out_content), null,
                        getString(R.string.ok), true,
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Utils.sUser = new User();
                                ACache.get(MyApplication.getmContext()).put(Constant.ACacheKey.KEY_USER, Utils.sUser, 1);
                                mPresenter.recycleToken(Utils.token);
                                Utils.token = null;
                                Intent intent = new Intent(SettingsActivity.this, LoginActivity.class);

                                intent.putExtra(IntentParams.LOGIN_FROM, LoginEvent.FROM_LOGIN);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

                                SettingsActivity.this.startActivity(intent);

                                SettingsActivity.this.finish();
                            }
                        });
                break;
        }
    }

    @Override
    public void onClickLeftView() {
        finish();
    }

    @Override
    public void onClickRightView() {

    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.scale_dialog_out);
    }

    @Override
    public void recycleTokenSuccess() {

    }

    @Override
    public void getCacheLengthSuccess(String length) {
        mSettingsCacheText.setText(length);
    }

    @Override
    public void deleteCacheSuccess() {
        mSettingsCacheText.setText("0kb");
        if(deleteCacheDialog != null) {
            deleteCacheDialog.dismiss();
            deleteCacheDialog = null;
        }
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void dismissProgress() {

    }

    @Override
    public void error(Throwable throwable) {

    }
}
